<?php
/*
Plugin Name: Synergy Announcements
Plugin URI: http://
Description: Plugin is counting bots visits
Author: Jaspreet Kaur Deol, Maia Engeli
Version: 1.0
Author URI: http://ditio.net
*/

// function widget_syn_announcement() {
// 	global $wpdb;
// 	echo $before_widget; 
//  	echo $before_title . 'Announcements' . $after_title;
// 
// 	//echo "Members: <br />";
// 	$result = $wpdb->get_results("SELECT `TEXT` FROM `{$wpdb->prefix}announcement`"); 
// 	foreach ($result as $r) {
// 	 	echo "<p>\n";
// 		echo ('<div id="text">'. $r->TEXT . "</div>\n");
// 	 	echo "</p>\n";
// 	}
// 	 
// 	echo $after_widget; 
// }

// register_sidebar_widget('announcement', 'widget_syn_announcement');

function announcement_install()
{
    global $wpdb;
    $table = $wpdb->prefix."announcement";
	
	 $structure = "DROP TABLE $table;";
    $wpdb->query($structure);
 
    $structure = "CREATE TABLE $table (
        ID     INT(9)       NOT NULL AUTO_INCREMENT,
        TEXT   VARCHAR(512) NOT NULL,
        DATE   DATETIME,
        PUBLIC TINYINT(1)   NOT NULL DEFAULT '0',
        UNIQUE KEY id (id)
    );";
    
    $wpdb->query($structure);
 
    // Populate table
    $wpdb->query("INSERT INTO $table(TEXT)
        VALUES('The repository is now compatible with new resource types ... ')");
    $wpdb->query("INSERT INTO $table(TEXT)
        VALUES('Need more space? We purchased additional memory, please contact the site administrator if you would like a higher quota.')");
    $wpdb->query("INSERT INTO $table(TEXT)
        VALUES('About Industry: We got some information to share about our new industry relationships. Please find more at the following link: <a href=\"somesite.com\">Industry Information</a>')");
}

add_action('activate_syn_announcement/syn_announcement.php', 'announcement_install');


// ADMIN MENU AND PAGE

add_action('admin_menu', 'syn_announcements_menu');

function syn_announcements_menu() {
  add_menu_page('Edit announcements', 'Announcements', 'manage_options', 'syn_announcements', 'edit_announcements');
}

function edit_announcements() {
  	global $wpdb;
	$editId = -1;
	
?>
<script type="text/javascript">
<!--
	function announcementrule(id, action, id2) {
		if (action == "syn_announcement_delete") {
			if (!confirm("Are you sure you want to delete this announcement?") )
     			return;
     	}
		var inputId = document.createElement("INPUT");
		inputId.type = "hidden";
		inputId.value = id;
		inputId.name = action;
		inputId.id = action;
		document.getElementById("hiddenForm").appendChild(inputId);
		if (id2 != 0) {
			var inputId2 = document.createElement("INPUT");
			inputId2.type = "hidden";
			inputId2.value = id2;
			inputId2.name = action + "2";
			inputId2.id = action + "2";
			document.getElementById("hiddenForm").appendChild(inputId2);
		}
		document.getElementById("hiddenForm").submit();
	}
-->
</script>


<?php	

  eval_anouncement_input(); // evaluate any $_POST queries that INSERT or UPDATE rules
  echo '<div class="wrap">';
  echo '<h1>ANNOUNCEMENTS</h1>';
  
  if (isset($_POST['syn_announcement_edit'])) {
	  
  		$editId = $_POST['syn_announcement_edit'];
  		$text = $wpdb->get_var("SELECT `TEXT` FROM `{$wpdb->prefix}announcement` WHERE `ID` = $editId;");
		// echo $text;
  }
  
  if ($editId == -1) {
?>
	<h2>Add a new announcement</h2>
	<form method="post" action="">
	  <textarea name="syn_newannouncement" rows="4" id="syn_newannouncement" style="width:95%"></textarea><br />
	  <input type='submit' name='syn_announcement_add' value='add' />
	</form>
    
<?php  } else {  ?>

	<h2>Edit announcement</h2>
	<form method="post" action="">
	  <textarea name="syn_announcement_edittext" rows="4" style="width:95%"><?php echo $text; ?></textarea>
	  <input type='hidden' name='syn_announcement_editid' value=<?php echo $editId; ?> />
 	  <input type='submit' name='syn_announcement_changetext' value='submit' />
	</form>
<?php

 	}
   show_announcements();   // Display the announcements from the database
   echo '</div>';
 }
 
// this displays the existing rules below the edit form
 function show_announcements() {
 	global $wpdb;
   
 	echo"<p>&nbsp;</p>";
 	echo "<h2>Current Announcement</h2>\n";

	$result = $wpdb->get_results("SELECT `ID`, `TEXT` FROM `{$wpdb->prefix}announcement` ORDER BY `ID`"); 
	echo '<form method="post" action="">' . "\n";
	echo "<ol style=\"padding-left:16px\">\n";
	
	$previd = -1;
	$i = 0;
	foreach ($result as $nextr) {
		if ($i == 2) {
			echo "<h3>Recent Announcements</h3>\n";
		}
		if ($i != 0) {
			echo "<li style='padding-bottom:6px'>$r->TEXT<br />\n";
			echo "<a  href='javascript:announcementrule($r->ID,\"syn_announcement_edit\", 0);' style='font-size:0.9em'>edit</a>\n";
			echo " | <a  href='javascript:announcementrule($r->ID, \"syn_announcement_delete\", 0);' style='font-size:0.9em'>delete</a>\n";
			if ($i != 1)  echo " | <a  href='javascript:announcementrule($r->ID,\"syn_announcement_swap\",$previd);' style='font-size:0.9em'>move up</a>\n" ;
			if ($i != $count) echo " | <a  href='javascript:announcementrule($r->ID,\"syn_announcement_swap\",$nextr->ID);' style='font-size:0.9em'>move down </a> </li>\n";
			$previd = $r->ID;
		 }
		$r=$nextr; 
		$i++;
	}

	echo "<li style='padding-bottom:6px'>$r->TEXT<br />\n";
	echo "<a  href='javascript:announcementrule($r->ID, \"syn_announcement_edit\", 0);' style='font-size:0.9em'>edit</a>\n";
	echo " | <a  href='javascript:announcementrule($r->ID, \"syn_announcement_delete\", 0);' style='font-size:0.9em'>delete</a>\n";
	if ($i != 1)  echo " | <a  href='javascript:announcementrule($r->ID, \"syn_announcement_swap\", $previd);' style='font-size:0.9em'>move up</a>\n" ;

	echo "</ol></form>\n";
	
	// a hidden form to send input via javascript
	echo '</div>';
	echo	'<div style="display: none;">';
	echo	'<form method="post" action="" id="hiddenForm"></form>';
	echo "</div>\n";

 }
 
// this function looks for $_POST and does the UPDATE/INSERT queries
function eval_anouncement_input() {
  	 global $wpdb;
 	
 	// ADD a new rule
	if(isset($_POST[syn_announcement_add]) && $_POST[syn_announcement_add] == "add") {
		$text = $_POST['syn_newannouncement'];
		if ($text != "") {
			$wpdb->query("INSERT INTO `{$wpdb->prefix}announcement`(TEXT, DATE)   
						VALUES('$text', NOW())");
		}
 	}
	
	// CHANGE/EDIT a rule
	else if(isset($_POST['syn_announcement_changetext'])) {
		$id = $_POST['syn_announcement_editid'];
		$text = $_POST['syn_announcement_edittext'];
		if ($text != "") {
			$wpdb->query("UPDATE `{$wpdb->prefix}announcement` 
						  SET `TEXT` = '$text', `DATE` = NOW() WHERE `ID` = $id;");
						
		}
	}
	
	// DELETE A RULE (javascript has asked for confirmation)
	else if(isset($_POST['syn_announcement_delete'])) {
		$id = $_POST['syn_announcement_delete'];
		$wpdb->query("DELETE from `{$wpdb->prefix}announcement` WHERE `ID` = $id;");
	}

	// SWAP TWO RULES (javascript has asked for confirmation)
	else if(isset($_POST['syn_announcement_swap'])) {
		$id = $_POST['syn_announcement_swap'];
		$id2 = $_POST['syn_announcement_swap2'];
		$wpdb->query("UPDATE `{$wpdb->prefix}announcement` SET `ID` = -1 WHERE `ID` = $id");
		$wpdb->query("UPDATE `{$wpdb->prefix}announcement` SET `ID` = $id WHERE `ID` = $id2");
		$wpdb->query("UPDATE `{$wpdb->prefix}announcement` SET `ID` = $id2 WHERE `ID` = -1");						
	}

}

?>

